/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 * 
 *	Created on 2010-12-30 下午04:37:26
 */
package cn.com.ccxe.core.crawl.article;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.ccxe.core.crawl.CrawlExpressionCache;
import cn.com.ccxe.core.entity.Article;
import cn.com.ccxe.core.entity.ArticleLink;
import cn.com.ccxe.core.entity.CrawlExpression;
import cn.com.ccxe.core.entity.WebData;

/**
 * 文章信息内容提取。
 * ps:非线程安全
 * @author hetao
 */
public class ArticleExtractor {

	private Document 			document ;
	final Logger 				logger = LoggerFactory.getLogger(ArticleExtractor.class);
	private SimpleDateFormat 	nomalFormat = new SimpleDateFormat("yyyyMMdd");
			
	public ArticleExtractor(WebData data) {
		document = Jsoup.parse(data.getHtml(), data.getLink().getPath());
	}
	public Article extractor(ArticleLink link) {
		Article a = new Article();
		a.setContent(document.body().text());
		if(StringUtils.isEmpty(link.getTitle())) {
			a.setTitle(document.title());
		}
		a.setUrl(document.baseUri());
		String date = extractorDate(link.getModuleId());
		a.setReleaseDate(date);
		return a;
	}
	
	private String extractorDate(Integer key) {
		CrawlExpression expre = CrawlExpressionCache.get(key);
		Elements elements = document.select(expre.getDateExpre());
		if(elements == null) {
			return "";
		}
		String strDate = elements.text();
		Date date = null;
		try {
			if(!StringUtils.isEmpty(strDate)) {
				SimpleDateFormat dateFormat = new SimpleDateFormat(expre.getDatePattern());
				date = dateFormat.parse(strDate);
				return nomalFormat.format(date);
			} else {
				System.out.println("时间为空，无法解析！");
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}
}
